// Observer.h

#ifndef OBSERVER_H
#define OBSERVER_H

#include "Observable.h"
#include "MineGrid.h"
#include <vector>

/**
* Holds and manages subscribers (called "Observable").
* This is an implementation of the observer pattern.
*/
class Observer
{
public:
	/**
	* Constructor.
	*/
	Observer() { }

	/**
	* Destructor.
	*/
	~Observer() { detacheAll(); }

	/**
	* Attaches the observable to the list.
	*/
	void attach( Observable* observable )
	{
		observables.push_back( observable );
	}

	/**
	* Detache the observable from the list.
	*/
	void detache( const Observable* observable )
	{
		std::vector<Observable*>::iterator itObservables;
		for( itObservables = observables.begin() ; itObservables != observables.end() ; ++itObservables )
			// We found the observable, erase it!
			if( *itObservables == observable )
				observables.erase( itObservables );
	}

	/**
	* Detaches all the observables from the list.
	*/
	void detacheAll() {	observables.clear(); }

	/**
	* Notifies the subscribers.
	*/
	void notifyAll()
	{
		std::vector<Observable*>::iterator itObservables;
		for( itObservables = observables.begin() ; itObservables != observables.end() ; ++itObservables )
			(*itObservables)->notify();
	}
  
private:
	// Keeps track of the observables.
	std::vector<Observable*> observables;
};

#endif // OBSERVER_H
